package ru.papergames.battleserver.model.battle.turnmanager;

import ru.papergames.Config;
import ru.papergames.battleserver.model.battle.AbstractBattle;

/**
 * @author: atroshanin
 * Date: 10.04.2010
 * Time: 23:37:07
 */
public class TurnManager {

    private static final TurnManager instance = new TurnManager();

    private TurnControllerThreadUpgr [] turnControllerThreads = new TurnControllerThreadUpgr[Config.TURN_CONTROLLER_THREAD_COUNT];

    private TurnManager() {
        for (int i=0; i< turnControllerThreads.length; i++) {
            turnControllerThreads[i] = new TurnControllerThreadUpgr(i);
        }
    }

    public static TurnManager getInstance() {
        return instance;
    }

    public boolean addBattleInTurnProcess(AbstractBattle battle) {
        for (int i=0; i<3; i++) {
            for (TurnControllerThreadUpgr t : turnControllerThreads) {
                if (!t.isFull()) {
                    if (t.addBattleToTurnProcess(battle)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public void start() {
        for (TurnControllerThreadUpgr t : turnControllerThreads) {
            t.start();
        }
    }

    public void shutDown() {
        for (TurnControllerThreadUpgr t : turnControllerThreads) {
            t.shutDown();
        }
    }
}
